const express = require('express')
const { readFileSync, writeFileSync } = require("fs");
const { resolve } = require("path");

const router = express.Router()
router.post("/post_comments", (req, res) => {
    const data = req.body.data;
    const comments = JSON.parse(
        readFileSync(resolve(__dirname, "../../data/commemt/data.json"), "utf8")
    );
    data.id = comments.length + 1;
    comments.push(data);
    writeFileSync(resolve(__dirname, "../../data/commemt/data.json"), JSON.stringify(comments));
    res.json({
        code: 200,
        msg: "ok",
        data: {
            comments,
        },
    });
});

//暴露 router
module.exports = router;